home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / USDebtWatch / unix-version / xdebt92.c < prev   
C/C++ Source or Header  |  1995-06-12  |  6KB  |  214 lines

  1. /* Displays the current national debt in a window, updated once a second.
  2.    Public domain.  By Jamie Zawinski <jwz@lucid.com>.
  3.    (you may need to add -DSYSV if timelocal is undefined -- tom)
  4.  
  5.     cc -O -o xdebt xdebt-new.c -lXaw -lXmu -lXt -lX11 -lXext
  6.  
  7.    More mods by sonroc@nemesis.zycad.com [a/k/a twk@zycad.com] to include
  8.    per-capita, also small square vs big rect; changed number printing style.
  9.  
  10.     Theoretically, you can run it as:
  11.         xdebt [-g +XCOORD+YCOORD] [-update SECS] [-font FONT]
  12.  
  13.     I haven't tested that -font works, but I use
  14.         xdebt -g +1080+360
  15.     routinely and
  16.         xdebt -update 1
  17.     also works for me.  Your mileage may vary....
  18.             --sonroc
  19.  
  20.    More mods by Perry Metzger, (pmetzger@shearson.com); reverted to a
  21.    big rectangle, eliminated silly cents field, uped the updates to
  22.    once a second, picked a bigger font, changed the labels to suit my
  23.    tastes. Easy to change, though.
  24.  
  25. note by Erik Sowa (sowa@netcom.com)
  26.  
  27. ultrix 4.2 compilation command:
  28. cc -DSYSV -O -o xdebt92 xdebt92.c -I/usr/include/mit -lXaw -lXmu -lXt -lX11 -lXext
  29.  
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <X11/Xos.h>
  34. #include <X11/Intrinsic.h>
  35. #include <X11/Xaw/Label.h>
  36.  
  37. #ifdef SYSV
  38. #define timelocal mktime
  39. #endif
  40.  
  41. typedef double debt_t;
  42. typedef double pop_t;
  43.  
  44. /* The US Population, and its rate of increase, as of July 1, 1990        */
  45. /* (in people/second) based on numbers from the CIA's World Factbook 1990 */
  46. /*  .... thanks to tom@genie.slhs.udel.edu                                */
  47. #define POP          250410000.0            /* people */
  48. #define POP_DELTA    0.0714151266        /* increase in people per second */
  49. static struct tm pop_tm = { 0, 0, 0, 1, 6, 90 };
  50.  
  51. /* The US National Debt, and its rate of increase as of June 30, 1992      */
  52. /* (in $/second) based on numbers from the 5 July 1992 Albuquerque Journal */
  53. /*  .... thanks to Brooke King brooke@fuchsia.albuq.ingr.com               */
  54. #define DEBT        3965170506502.395    /* in dollars */
  55. #define DEBT_DELTA    14132.887            /* Dollars per second */
  56. static struct tm debt_tm = { 0, 0, 0, 30, 05, 92 };
  57.  
  58.  
  59. debt_t
  60. national_debt_at (now)
  61.      time_t now;
  62. {
  63.   time_t debt_date = timelocal (&debt_tm);
  64.   time_t seconds_since_then = now - debt_date;
  65.   debt_t delta_since_then = DEBT_DELTA * seconds_since_then;
  66.   return DEBT + delta_since_then;
  67. }
  68.  
  69. pop_t
  70. national_pop_at (now)
  71.      time_t now;
  72. {
  73.   time_t pop_date = timelocal (&pop_tm);
  74.   time_t seconds_since_then = now - pop_date;
  75.   pop_t delta_since_then = POP_DELTA * seconds_since_then;
  76.   return POP + delta_since_then;
  77. }
  78.  
  79.  
  80. void
  81. pretty_number(n, buf)
  82.   double n;
  83.   char buf[256];
  84. {
  85.     char tmpBuf[256], *cp, *bp;
  86.     int  len, count, digits;
  87.  
  88. /*    sprintf(tmpBuf, "%.2lf", n);*/
  89.     sprintf(tmpBuf, "%.0lf", n);
  90.     len = strlen(tmpBuf);
  91.     cp = tmpBuf + len - 1;
  92.     bp = buf;
  93.  
  94.     /* copy over the cents and first 3 digits */
  95.     /* *bp++ = *cp--; *bp++ = *cp--; *bp++ = *cp--; */
  96.     *bp++ = *cp--; *bp++ = *cp--; *bp++ = *cp--;
  97.     digits = 7;
  98.  
  99.     /* Now every three digits gets a comma; at 10 digits insert a line at the
  100.         comma  */
  101.     while (cp >= tmpBuf)
  102.     {
  103.         /* MODIFY NEWLINE IN LARGE NUMBERS HERE */
  104. /*        if (digits > 10)
  105.         {
  106.             *bp++ = ' '; *bp++ = '\n'; digits=0;
  107.         }*/
  108.         *bp++ = ','; digits++;
  109.         for (count = 0; count < 3 && cp >= tmpBuf; count++)
  110.         {
  111.             *bp++ = *cp--; digits++;
  112.         }
  113.     }
  114.     *bp++ = '\0';
  115.     strcpy(tmpBuf, buf);
  116.  
  117.     len = strlen(tmpBuf);
  118.     for (count = 0; count < len; count++)
  119.     {
  120.         buf[(len-1) - count] = tmpBuf[count];
  121.     }
  122.     buf[len] = '\0';
  123. }
  124.  
  125. void
  126. construct_string (now, outBuf)
  127.      time_t now;
  128.      char   *outBuf;
  129. {
  130.   debt_t   debt = national_debt_at(now);
  131.   pop_t    pop  = national_pop_at(now);
  132.   char     debtBuf[256], shareBuf[256];
  133.  
  134.   pretty_number(debt, debtBuf);
  135.   pretty_number(debt/pop,  shareBuf);
  136.   sprintf(outBuf, "U.S. National Debt:\n$%s\nYour Share:\n$%s", debtBuf, shareBuf);
  137. }
  138.  
  139.  
  140. static int update;
  141.  
  142. /*   MODIFY FONT HERE   */
  143. static char *defaults[] = {
  144. /*  "*Label.font:    12x24",/*    /* was times-...-240 */
  145. /*  "*Label.font:    *-helvetica-bold-r-*-*-*-100-*-*-*-*-*-*",    /* was times-...-240 */
  146.   "*Label.font:         *-times-bold-r-*-*-*-180-*-*-*-*-*-*",
  147.   "*title:    xdebt92",
  148.   "*update:    1",
  149.   NULL
  150. };
  151.  
  152. static XrmOptionDescRec options [] = {
  153.   { "-update",    "*update",    XrmoptionSepArg, 0 }
  154. };
  155.  
  156.  
  157. static void
  158. get_update (dpy)    /* Easier than making a subclass... */
  159.      Display *dpy;
  160. {
  161.   char *name, *class, *type, buf1[255], buf2[255];
  162.   XrmValue value;
  163.   XtGetApplicationNameAndClass (dpy, &name, &class);
  164.   sprintf (buf1, "%s.update", name);
  165.   sprintf (buf2, "%s.Update", class);
  166.   XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value);
  167.   if (sscanf (value.addr, " %d ", &update) == 0 || update < 1)
  168.     {
  169.       fprintf (stderr, "%s: update must be a positive integer, not \"%s\"\n",
  170.            name, value.addr);
  171.       update = 1;
  172.     }
  173. }
  174.  
  175.  
  176. static void
  177. timer (w, id)
  178.      Widget w;
  179.      XtIntervalId id;
  180. {
  181.   char buf [255];
  182.   Arg av [10];
  183.   int ac = 0;
  184.   time_t now = time((time_t *)0);
  185.   construct_string (now, buf);
  186.   XtSetArg (av [ac], XtNlabel, buf); ac++;
  187.   XtSetValues (w, av, ac);
  188.   XtAppAddTimeOut (XtWidgetToApplicationContext (w), update * 1000, timer, w);
  189. }
  190.  
  191.  
  192. void
  193. main (argc, argv)
  194.      int argc;
  195.      char **argv;
  196. {
  197.   XtAppContext app;
  198.   Widget shell = XtAppInitialize (&app, "XDebt", options, XtNumber (options),
  199.                   &argc, argv, defaults, NULL, 0);
  200.   Widget label;
  201.   XEvent event;
  202.   if (argc > 1)
  203.     {
  204.       fprintf (stderr, "%s: unknown option %s\n", argv[0], argv[1]);
  205.       fprintf (stderr, "options: -update <seconds>\n\t -font <font>\n");
  206.       exit (-1);
  207.     }
  208.   label = XtCreateManagedWidget ("label", labelWidgetClass, shell, NULL, 0);
  209.   get_update (XtDisplay (label));
  210.   timer (label, 0);
  211.   XtRealizeWidget (shell);
  212.   XtAppMainLoop (app);
  213. }
  214.